package com.han.spider.safecounty;

import com.han.util.UnZipper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.nio.charset.StandardCharsets;
import java.time.Duration;

/**
 * @Author: Hanjiafeng
 * @Date: 2020/6/19
 * @Desc:
 */
public class Login {
    private static final Logger LOGGER = LoggerFactory.getLogger(Login.class);
    private final HttpClient httpClient;
    private final HttpRequest.Builder requestBuilder;
    private final String userName;
    /**
     * js引擎调用器
     */
    private final String password;

    public Login(String userName, String password) {

        this.userName = userName;
        this.password = password;

        HttpClient.Builder clientBuilder = HttpClient.newBuilder();
        httpClient = clientBuilder
                .build();

        requestBuilder = HttpRequest.newBuilder()
                .timeout(Duration.ofMinutes(1))
                .setHeader("Accept", "application/json, text/plain, */*")
                .setHeader("Accept-Encoding", "gzip, deflate, br")
                .setHeader("Accept-Language", "en,zh-CN;q=0.9,zh;q=0.8")
                .setHeader("Content-Type", "application/x-www-form-urlencoded")
                .setHeader("Sec-Fetch-Dest", "empty")
                .setHeader("Sec-Fetch-Mode", "cors")
                .setHeader("Sec-Fetch-Site", "same-origin")
                .setHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.92 Safari/537.36");

        //初始化js引擎调用器
//        jsInvoker = new JsInvoker("E:\\project\\java\\spider\\src\\main\\java\\com\\han\\spider\\safecounty\\js\\jsencrypt.js");
    }

    public String loginIn() throws IOException, InterruptedException {
        LOGGER.info("开始加密密码");
        //加密密码
//        password = encryptPassword();
        LOGGER.info("密码加密成功:" + password);
        LOGGER.info("开始登陆");
        HttpRequest.BodyPublisher postData = HttpRequest.BodyPublishers.ofString("userName=" + userName + "&password=" + password + "&code=&");
        HttpRequest httpRequest = getRequestBuilder()
                .uri(URI.create("https://ehome.21cn.com/cameraPlatform/mapi/user/loginConfirm"))
                .POST(postData)
                .build();

        HttpResponse<byte[]> byteResponse = getHttpClient().send(httpRequest, HttpResponse.BodyHandlers.ofByteArray());
        String body = new String(UnZipper.unGzipByBytes(byteResponse.body()), StandardCharsets.UTF_8);
        LOGGER.info("登陆返回内容:" + body);
        String cookie = byteResponse.headers().firstValue("set-cookie").orElseThrow();
        cookie += ";account=" + userName;
        LOGGER.info("获取cookie:" + cookie);
        return cookie;
    }

    public HttpClient getHttpClient() {
        return httpClient;
    }

    public HttpRequest.Builder getRequestBuilder() {
        return requestBuilder;
    }
}
